package com.shashidi.weather.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import com.android.tu.loadingdialog.LoadingDailog;
import com.gtdev5.geetolsdk.mylibrary.base.BaseGTActivity;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.shashidi.weather.R;
import com.shashidi.weather.util.AppManagerUtil;
import com.shashidi.weather.view.TitleView;

import butterknife.ButterKnife;

public abstract class BaseActivity extends BaseGTActivity implements View.OnClickListener {
    public Bundle save;
    public LoadingDailog dialog;
    private static final int DEFAULT_TIMEOUT = 6;

    protected TitleView title;

    private boolean showStatusBar = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        save = savedInstanceState;
        AppManagerUtil.instance().addActivity(this);
        //设置为无标题栏
        setRootView();
        ButterKnife.bind(this);
        getLoadingDailog();
        //公共title
        title = (TitleView) findViewById(R.id.title);
        title.setLeftButton(new TitleView.OnLeftButtonClickListener() {
            @Override
            public void onClick(View button) {
                finish();
            }
        });
        if (showStatusBar) {
            ImmersionBar.with(this)
                    .statusBarColor(R.color.white)
                    .statusBarDarkFont(true)
                    .fitsSystemWindows(true)
                    .navigationBarDarkIcon(true)
                    .init();
        }
        initTitle();
        initView();
        initData();
    }
    public void getLoadingDailog() {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutside(true);

        dialog = loadBuilder.create();
    }

    protected final void logShow(String msg) {
        Log.e("weather", msg);
    }

    protected abstract void initView();

    public abstract void initTitle();

    public abstract void setRootView();

    public abstract void initData();

    protected final String getTextByEditText(EditText et) {
        return et.getText().toString().trim();
    }

    @Override
    public void onBackPressed() {
        AppManagerUtil.instance().finishActivity(this);
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManagerUtil.instance().finishActivity(this);

    }

    @Override
    public void finish() {
        hideSoftInputFromWindow(this);
        super.finish();
    }

    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    private void hideSoftInputFromWindow(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity
                .getSystemService(INPUT_METHOD_SERVICE);
        try {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
